/**
 * API client implementation.
 */

#include "client.h"
#include <stdexcept>
#include <iostream>

namespace api {

Client::Client(const std::string& base_url, const std::string& api_key)
    : base_url_(base_url), api_key_(api_key), is_closed_(false) {
    
    // Remove trailing slash
    if (!base_url_.empty() && base_url_.back() == '/') {
        base_url_.pop_back();
    }
}

Client::~Client() {
    if (!is_closed_) {
        close();
    }
}

std::string Client::build_url(const std::string& endpoint) {
    std::string url = base_url_;
    if (!endpoint.empty() && endpoint[0] != '/') {
        url += "/";
    }
    url += endpoint;
    return url;
}

std::map<std::string, std::string> Client::get_data(const std::string& endpoint) {
    if (is_closed_) {
        throw std::runtime_error("Client is closed");
    }
    
    std::string url = build_url(endpoint);
    
    // In a real implementation, this would make an HTTP GET request
    std::map<std::string, std::string> response;
    response["status"] = "success";
    response["url"] = url;
    response["method"] = "GET";
    
    return response;
}

std::map<std::string, std::string> Client::post_data(const std::string& endpoint, 
                                                     const std::map<std::string, std::string>& data) {
    if (is_closed_) {
        throw std::runtime_error("Client is closed");
    }
    
    std::string url = build_url(endpoint);
    
    // In a real implementation, this would make an HTTP POST request
    std::map<std::string, std::string> response;
    response["status"] = "success";
    response["url"] = url;
    response["method"] = "POST";
    response["data_size"] = std::to_string(data.size());
    
    return response;
}

std::map<std::string, std::string> Client::upload_file(const std::string& endpoint, 
                                                       const std::string& file_path) {
    if (is_closed_) {
        throw std::runtime_error("Client is closed");
    }
    
    std::string url = build_url(endpoint);
    
    // In a real implementation, this would upload the file
    std::map<std::string, std::string> response;
    response["status"] = "success";
    response["url"] = url;
    response["method"] = "POST";
    response["file_path"] = file_path;
    
    return response;
}

void Client::close() {
    is_closed_ = true;
}

} // namespace api